import Sequelize from 'sequelize';
import { DATABASE_NAME, DATABASE_HOST, DATABASE_USER, DATABASE_PASS, DATABASE_PORT } from '../config';

const sequelize = new Sequelize(
  process.env.TEST_DATABASE || DATABASE_NAME, // 数据库名
  DATABASE_USER, // 用户名
  DATABASE_PASS, // 用户密码
  {
    dialect: 'postgres', // 数据库使用postgres
    host: DATABASE_HOST, // 数据库服务器ip
    port: DATABASE_PORT, // 数据库服务器端口
    define: {
      underscored: true // 字段以下划线（_）来分割（默认是驼峰命名风格）
    },
    logging: false // 关闭日志输出，如和将日志输出写入日志文件请参考 http://blog.csdn.net/ltfyxkfh/article/details/41653511 (sql) => { console.log(sql) }
  }
);

const models = {
  AdminUser: sequelize.import('./admin/user'),
  AdminRole: sequelize.import('./admin/role'),
  AdminPermission: sequelize.import('./admin/permission'),
  AdminPermissionClassify: sequelize.import('./admin/permission-classify'),
  AdminAccessRouter: sequelize.import('./admin/access-router')
};

Object.keys(models).forEach(key => {
  if ('associate' in models[key]) {
    models[key].associate(models);
  }
});

export { sequelize };
export default models;
// 老的 mongo 表，后续一个个迁移
export { Banner } from './banner';
export { Advisement } from './advisement';
export { Theme } from './theme';
export { ThemeBook } from './theme-book';
export { HotSearch } from './hot-search';
export { Book } from './book';
export { Chapter } from './chapter';
export { User } from './user';
export { BookList } from './booklist';
export { Comment } from './comment';
export { Good } from './good';
export { Pay } from './pay';
export { Buy } from './buy';
export { Charge } from './charge';
export { Attendance } from './attendance';
export { Setting } from './setting';
export { Share } from './share';
export { Award } from './award';
export { Secret } from './secret';
export { FriendHelp } from './friend-help';
export { FriendHelpBook } from './friend-help-book';
export { Dialog } from './dialog';
export { Notice } from './notice';
export { Member } from './member';
export { Logger } from './logger';
export { Feedback } from './feedback';
export { Classify } from './classify';
